package com.axin.web.service;

import com.axin.blog.api.RemoteWebUserService;
import com.axin.blog.api.domain.WebUser;
import com.axin.common.core.domain.web.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author axin
 * @Date 2021/11/11 21:51
 **/
@Service
public class WebUserService {


    @Autowired
    private RemoteWebUserService userService;

    @Autowired
    private WebTokenService webTokenService;

    /**
     * 更新用户信息
     *
     * @param edit
     * @param request
     */
    public void editUserInfo(WebUser edit, HttpServletRequest request) {
        // 更新数据库
        AjaxResult ajaxResult = userService.updateUserProfile(edit);
        if (ajaxResult.isSuccess()) {

            // 更新redis
            String token = webTokenService.getToken(request);
            WebUser user = userService.infoByUsername(edit.getUserName()).getData();
            webTokenService.refreshToken(token, user);

        }

    }


    /**
     * 重置用户密码
     *
     * @param loginUser 用户信息
     * @param password 密码
     * @return 结果
     */
    public void resetUserPwd(WebUser loginUser, String password, HttpServletRequest request) {
        // 更新数据库
        loginUser.setPassword(password);

        AjaxResult ajaxResult = userService.updateUserProfile(loginUser);
        if (ajaxResult.isSuccess()) {

            // 更新redis
            String token = webTokenService.getToken(request);
            WebUser user = userService.infoByUsername(loginUser.getUserName()).getData();
            webTokenService.refreshToken(token, user);

        }
    }
}
